package com.rd.student.ruanjian.class4.lichunquan.L_2023_7_11;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        Scanner sc = new Scanner(System.in);
        Student s;
        boolean b = true;
        arrayList.add(new Student("李春泉","软四",20));
        arrayList.add(new Student("周子贺","软四",21));
        arrayList.add(new Student("王星凯","软四",19));

        while(b == true){
            System.out.println("-----------------");
            System.out.println("按1添加学生");
            System.out.println("按2按年龄从小到大排序");
            System.out.println("-----------------");

            switch(sc.nextInt()){
                //添加学生
                case 1:
                    System.out.println("添加信息有姓名，班级，年龄");
                    System.out.println("添加格式例如:张三，软四 ，21");
                    arrayList.add(new Student(sc.next(),sc.next(),sc.nextInt()));break;
                //按年龄从小到大排序
                case 2: sortOfAge(arrayList);break;
                //结束进程
                case 3: b = false;break;
                default: System.out.println("重新输入");
            }
        }
    }
    public static void sortOfAge(ArrayList arrayList){
        Collections.sort(arrayList);
        for (Object s: arrayList) {
            System.out.println(s.toString());
        }

    }

}
class Student implements Comparable{
    private String name;
    private String cls;
    private int age;
    private Student s;
    Student(){}
    public Student(String name, String cls, int age) {
        this.name = name;
        this.cls = cls;
        this.age = age;
    }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getCls() { return cls; }
    public void setCls(String cls) { this.cls = cls; }
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }

    @Override
    public String toString() {
        return "姓名:" + name + ", 班级:" + cls + ", 年龄" + age ;
    }

    @Override
    public int compareTo(Object o) {
        s = (Student) o;
        if(s.age > this.age){
            return -1;
        }else if(s.age < this.age){
            return 1;
        }else {
            return 0;
        }
    }
}

